package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.WcInsuranceTypeService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.WcInsuranceTypeDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing WcInsuranceType.
 */
@RestController
@RequestMapping("/api")
public class WcInsuranceTypeResource {

    private final Logger log = LoggerFactory.getLogger(WcInsuranceTypeResource.class);

    private static final String ENTITY_NAME = "wcInsuranceType";

    private final WcInsuranceTypeService wcInsuranceTypeService;

    public WcInsuranceTypeResource(WcInsuranceTypeService wcInsuranceTypeService) {
        this.wcInsuranceTypeService = wcInsuranceTypeService;
    }

    /**
     * POST  /wc-insurance-types : Create a new wcInsuranceType.
     *
     * @param wcInsuranceTypeDTO the wcInsuranceTypeDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new wcInsuranceTypeDTO, or with status 400 (Bad Request) if the wcInsuranceType has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/wc-insurance-types")
    @Timed
    public ResponseEntity<WcInsuranceTypeDTO> createWcInsuranceType(@RequestBody WcInsuranceTypeDTO wcInsuranceTypeDTO) throws URISyntaxException {
        log.debug("REST request to save WcInsuranceType : {}", wcInsuranceTypeDTO);
        if (wcInsuranceTypeDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new wcInsuranceType cannot already have an ID")).body(null);
        }
        WcInsuranceTypeDTO result = wcInsuranceTypeService.save(wcInsuranceTypeDTO);
        return ResponseEntity.created(new URI("/api/wc-insurance-types/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /wc-insurance-types : Updates an existing wcInsuranceType.
     *
     * @param wcInsuranceTypeDTO the wcInsuranceTypeDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated wcInsuranceTypeDTO,
     * or with status 400 (Bad Request) if the wcInsuranceTypeDTO is not valid,
     * or with status 500 (Internal Server Error) if the wcInsuranceTypeDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/wc-insurance-types")
    @Timed
    public ResponseEntity<WcInsuranceTypeDTO> updateWcInsuranceType(@RequestBody WcInsuranceTypeDTO wcInsuranceTypeDTO) throws URISyntaxException {
        log.debug("REST request to update WcInsuranceType : {}", wcInsuranceTypeDTO);
        if (wcInsuranceTypeDTO.getId() == null) {
            return createWcInsuranceType(wcInsuranceTypeDTO);
        }
        WcInsuranceTypeDTO result = wcInsuranceTypeService.save(wcInsuranceTypeDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, wcInsuranceTypeDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /wc-insurance-types : get all the wcInsuranceTypes.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of wcInsuranceTypes in body
     */
    @GetMapping("/wc-insurance-types")
    @Timed
    public ResponseEntity<List<WcInsuranceTypeDTO>> getAllWcInsuranceTypes(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of WcInsuranceTypes");
        Page<WcInsuranceTypeDTO> page = wcInsuranceTypeService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/wc-insurance-types");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /wc-insurance-types/:id : get the "id" wcInsuranceType.
     *
     * @param id the id of the wcInsuranceTypeDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the wcInsuranceTypeDTO, or with status 404 (Not Found)
     */
    @GetMapping("/wc-insurance-types/{id}")
    @Timed
    public ResponseEntity<WcInsuranceTypeDTO> getWcInsuranceType(@PathVariable Long id) {
        log.debug("REST request to get WcInsuranceType : {}", id);
        WcInsuranceTypeDTO wcInsuranceTypeDTO = wcInsuranceTypeService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(wcInsuranceTypeDTO));
    }

    /**
     * DELETE  /wc-insurance-types/:id : delete the "id" wcInsuranceType.
     *
     * @param id the id of the wcInsuranceTypeDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/wc-insurance-types/{id}")
    @Timed
    public ResponseEntity<Void> deleteWcInsuranceType(@PathVariable Long id) {
        log.debug("REST request to delete WcInsuranceType : {}", id);
        wcInsuranceTypeService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/wc-insurance-types?query=:query : search for the wcInsuranceType corresponding
     * to the query.
     *
     * @param query the query of the wcInsuranceType search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/wc-insurance-types")
    @Timed
    public ResponseEntity<List<WcInsuranceTypeDTO>> searchWcInsuranceTypes(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of WcInsuranceTypes for query {}", query);
        Page<WcInsuranceTypeDTO> page = wcInsuranceTypeService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/wc-insurance-types");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
